
package com.windgo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.windgo.bean.*;
import com.windgo.bean.bo.*;
import com.windgo.bean.vo.*;
import com.windgo.bean.common.BasePageInfo;
import com.windgo.bean.common.CommonData;
import com.windgo.mapper.*;
import com.windgo.util.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Author:ZhouXin
 * @Date:2022/10/13 11:35
 * @Description:
 **/
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    MarketOrderMapper marketOrderMapper;

    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;

    @Autowired
    MarketUserMapper marketUserMapper;

    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    @Autowired
    MarketCartMapper marketCartMapper;

    @Override
    public CommonData<MarketOrder> list(BasePageInfo info, Integer userId, String orderSn, Short[] orderStatusArray, Date start, Date end) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketOrderExample example = new MarketOrderExample();

        //排序
        example.setOrderByClause(info.getSort() + " " + info.getOrder());

        MarketOrderExample.Criteria criteria = example.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }

        if (!StringUtil.isEmpty(orderSn)) {
            criteria.andOrderSnEqualTo(orderSn);
        }

//        if (orderStatusArray!=null){
//            //criteria.andOrderStatusIn(orderStatusArray);
//
//        }
        if (orderStatusArray != null) {
            for (Short aShort : orderStatusArray) {
                if (aShort != null) {
                    criteria.andOrderStatusEqualTo(aShort);
                }
            }
        }

        //start和end时间查的是addTime
        if (start != null && end != null) {
            criteria.andAddTimeBetween(start, end);
        }

        List<MarketOrder> list = marketOrderMapper.selectByExample(example);

        PageInfo<MarketOrder> pageInfo = new PageInfo<>(list);


        return CommonData.data(pageInfo);
    }

    /**
     * 显示订单详情
     *
     * @param id:order订单表的id
     * @return
     */
    @Override
    public OrderDetailVo detail(Integer id) {
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        //获取List<> orderGoods信息,id就是order_goods表中order_id
        MarketOrderGoodsExample example = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(id);
        List<MarketOrderGoods> list = marketOrderGoodsMapper.selectByExample(example);
        System.out.println(list);
        //赋值
        orderDetailVo.setOrderGoods(list);


        //查询用户信息
        //根据id获取userId
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(id);
        Integer userId = marketOrder.getUserId();
        //根据userId去user表中查找信息
        MarketUser user = marketUserMapper.selectByPrimaryKey(userId);

        //赋值
        orderDetailVo.setUser(user);

        //赋值订单信息
        orderDetailVo.setOrder(marketOrder);

        return orderDetailVo;
    }

    /**
     * 根据id删除订单
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        marketOrderMapper.deleteByPrimaryKey(id);
    }

    /**
     * 发货，修改shipChannel,shipSn  和订单状态改为301
     *
     * @param orderShipVo
     */
    @Override
    public void ship(OrderShipVo orderShipVo) {
        //拿取数据
        Integer orderId = orderShipVo.getOrderId();
        String shipChannel = orderShipVo.getShipChannel();
        String shipSn = orderShipVo.getShipSn();
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        marketOrder.setShipChannel(shipChannel);
        marketOrder.setShipSn(shipSn);
        //Short aShort = new Short(301);
        short a = 301;
        marketOrder.setOrderStatus(a);
        Date date = new Date();
        marketOrder.setUpdateTime(date);
        //添加发货时间
        marketOrder.setShipTime(date);
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
    }

    /**
     * 退款功能，修改订单状态为203:已退款，修改退款金额
     *
     * @param orderRefundBo
     */
    @Override
    public void refund(OrderRefundBo orderRefundBo) {
        //获取信息
        Integer id = orderRefundBo.getOrderId();
        BigDecimal refundMoney = orderRefundBo.getRefundMoney();
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(id);
        marketOrder.setRefundAmount(refundMoney);
        short a = 203;
        marketOrder.setOrderStatus(a);
        Date date = new Date();
        marketOrder.setUpdateTime(date);
        marketOrder.setRefundTime(date);
        marketOrder.setRefundTime(date);
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
    }

    /**
     * 回复商品评论:给comment表中的admin_content赋值,增加更新的时间
     *
     * @param orderCommentReplyBo
     */
    @Override
    public void reply(OrderCommentReplyBo orderCommentReplyBo) {
        //获取
        Integer id = orderCommentReplyBo.getCommentId();
        String content = orderCommentReplyBo.getContent();

        MarketComment marketComment = new MarketComment();
        marketComment.setId(id);
        Date date = new Date();
        marketComment.setUpdateTime(date);
        marketComment.setAdminContent(content);
        marketCommentMapper.updateByPrimaryKeySelective(marketComment);
    }

    /**
     * 添加商品评论
     * 评论后order表中comments数量减一
     * @param wxCommentCreateBo
     */
    @Override
    public void wxComment(WxCommentCreateBo wxCommentCreateBo, Integer userId) {
        //获取参数，将参数给marketComment赋值
        MarketComment marketComment = new MarketComment();


        //valueId是order_goods表中的goodsId
        Integer orderGoodsId1 = wxCommentCreateBo.getOrderGoodsId();
        MarketOrderGoods marketOrderGoods1 = marketOrderGoodsMapper.selectByPrimaryKey(orderGoodsId1);
        Integer goodsId = marketOrderGoods1.getGoodsId();
        marketComment.setValueId(goodsId);
        marketComment.setContent(wxCommentCreateBo.getContent());
        marketComment.setStar(wxCommentCreateBo.getStar());
        marketComment.setHasPicture(wxCommentCreateBo.isHasPicture());
        marketComment.setPicUrls(wxCommentCreateBo.getPicUrls());
        Date date = new Date();
        marketComment.setAddTime(date);
        marketComment.setUpdateTime(date);
        // 用户表的id
        marketComment.setUserId(userId);
        marketCommentMapper.insertSelective(marketComment);

        //评论表新增评论信息之后，获取评论表的id
        Integer id = marketComment.getId();
        //id赋值给order_goods表中的comment  请求体中有orderGoodsId信息
        Integer orderGoodsId = wxCommentCreateBo.getOrderGoodsId();
        MarketOrderGoods marketOrderGoods = new MarketOrderGoods();
        marketOrderGoods.setId(orderGoodsId);
        marketOrderGoods.setComment(id);
        marketOrderGoodsMapper.updateByPrimaryKeySelective(marketOrderGoods);

        //TODO 评论完成后，order表中comments的数量也要--
        //根据orderGoodsId查orderId
        MarketOrderGoods orderGoods = marketOrderGoodsMapper.selectByPrimaryKey(orderGoodsId1);
        //根绝orderId查当前信息，获取到comments
        Integer orderId = orderGoods.getOrderId();
        //comments--
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderId);
        Short comments = marketOrder.getComments();
        short newComments=(short)(comments-1);
        //将新的comments赋值进去
        MarketOrder order = new MarketOrder();
        order.setId(orderId);
        order.setComments(newComments);
        marketOrderMapper.updateByPrimaryKeySelective(order);


    }

    /**
     * 根据showType显示不同类型的订单
     * 0:显示所有订单
     * 1:显示未付款的
     * 2:显示代发货的
     * 3:显示待收货的
     * 4:显示待评价的
     *
     * @param info
     * @param showType
     * @return
     */
    @Override
    public CommonData<WxOrderListVo> wxList(BasePageInfo info, Integer showType) {
        //创建响应的集合
        ArrayList<WxOrderListVo> data = new ArrayList<>();


        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        MarketUser primaryPrincipal1 = (MarketUser) principals.getPrimaryPrincipal();
        Integer userId = primaryPrincipal1.getId();


        //开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria2 = marketOrderExample.createCriteria();
        criteria2.andUserIdEqualTo(userId);
        criteria2.andDeletedEqualTo(false);
        List<MarketOrder> list = marketOrderMapper.selectByExample(marketOrderExample);
        //List<MarketOrder> list = marketOrderMapper.selectAllOrders();

        //对showType做判断
        if (showType == 0) {
            for (MarketOrder marketOrder : list) {
                //创建响应的对象
                WxOrderListVo wxOrderListVo = new WxOrderListVo();
                //创建响应体中WxOrderListHandOptionVo对象
                WxOrderListHandleOptionVo optionVo = new WxOrderListHandleOptionVo();
                //获取订单号
                Integer id = marketOrder.getId();
                wxOrderListVo.setId(id);
                //获取afterSaleStatus
                wxOrderListVo.setAftersaleStatus(marketOrder.getAftersaleStatus());
                //通过grouponPrice生成isGroupin
                BigDecimal grouponPrice = marketOrder.getGrouponPrice();
                String s = grouponPrice.toString();
                if (s.equals("0.00")) {
                    wxOrderListVo.setIsGroupin(false);
                }
                //orderSn
                String orderSn = marketOrder.getOrderSn();
                wxOrderListVo.setOrderSn(orderSn);
                //actualPrice
                BigDecimal actualPrice = marketOrder.getActualPrice();
                wxOrderListVo.setActualPrice(actualPrice);

                //构建orderStatusText:
                //eg:"orderStatusText": "已付款";
                //eg:"orderStatusText": "订单取消，退款中"
                //根据order_status的状态码进行显示
                //判断
                //新建了一个表market_order_handleoption
                //1.获取状态码
                Short orderStatus = marketOrder.getOrderStatus();
                //从刚刚创建的表中获取获取orderStatusText和handleOption
                //通过orderStatus去差信息
                MarketOrderHandleOption option = marketOrderMapper.selectAllHandlerOption(orderStatus);
                String orderStatusText = option.getOrderStatusText();
                wxOrderListVo.setOrderStatusText(orderStatusText);

                //封装handleOption
                Integer cancel = option.getCancel();
                Integer delete = option.getDelete();
                Integer pay = option.getPay();
                Integer comment = option.getComment();
                Integer confirm = option.getConfirm();
                Integer refund = option.getRefund();
                Integer reBuy = option.getReBuy();
                Integer afterSale = option.getAfterSale();

                optionVo = intAndBoolean(optionVo, cancel, delete, pay, comment, confirm, refund, reBuy, afterSale);
                wxOrderListVo.setHandleOption(optionVo);

                //根据id去order_goods中查找信息,order_id
                MarketOrderGoodsExample orderGoodsExample = new MarketOrderGoodsExample();
                MarketOrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();
                criteria.andOrderIdEqualTo(id);
                List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(orderGoodsExample);
                //封装
                wxOrderListVo.setGoodsList(marketOrderGoods);

                //添加到集合
                data.add(wxOrderListVo);
            }

            PageInfo<MarketOrder> pageInfo = new PageInfo<>(list);
            return new CommonData<>(
                    (int) pageInfo.getTotal(),
                    pageInfo.getPages(),
                    info.getLimit(),
                    info.getPage(),
                    data
            );
        }
        if (showType == 4) {
            //如果是显示代评价的，需要对order_goods表中获取comment字段，对这个字段做判断
            //根据showType进行模糊查询  order
            //对showType做处理
            int a = showType * 100;

            Integer statusLow = a;
            Integer statusTop = statusLow + 100;
            MarketOrderExample example = new MarketOrderExample();
            MarketOrderExample.Criteria criteria = example.createCriteria();
            criteria.andUserIdEqualTo(userId);
            criteria.andOrderStatusBetween(statusLow, statusTop);

            list = marketOrderMapper.selectByExample(example);
            for (MarketOrder marketOrder : list) {
                //创建响应的对象
                WxOrderListVo wxOrderListVo = new WxOrderListVo();
                //创建响应体中WxOrderListHandOptionVo对象
                WxOrderListHandleOptionVo optionVo = new WxOrderListHandleOptionVo();
                //获取订单号
                Integer id = marketOrder.getId();


                //新加的判断
                //去order_goods表中获取comment字段,若果comment字段为0,表示这个订单可以进行评价，则进行显示
                MarketOrderGoodsExample orderGoodsExample1 = new MarketOrderGoodsExample();
                MarketOrderGoodsExample.Criteria orderGoodsExampleCriteria = orderGoodsExample1.createCriteria();
                orderGoodsExampleCriteria.andOrderIdEqualTo(id);
                List<MarketOrderGoods> orderGoods = marketOrderGoodsMapper.selectByExample(orderGoodsExample1);
                //对获取到的orderGoods进行遍历，获取到comment字段
                for (MarketOrderGoods orderGood : orderGoods) {
                    Integer comment1 = orderGood.getComment();
                    //对comment做判断
                    if (comment1 == 0) {
                        wxOrderListVo.setId(id);
                        //获取afterSaleStatus
                        wxOrderListVo.setAftersaleStatus(marketOrder.getAftersaleStatus());
                        //通过grouponPrice生成isGroupin
                        BigDecimal grouponPrice = marketOrder.getGrouponPrice();
                        String s = grouponPrice.toString();
                        if (s.equals("0.00")) {
                            wxOrderListVo.setIsGroupin(false);
                        }
                        //orderSn
                        String orderSn = marketOrder.getOrderSn();
                        wxOrderListVo.setOrderSn(orderSn);
                        //actualPrice
                        BigDecimal actualPrice = marketOrder.getActualPrice();
                        wxOrderListVo.setActualPrice(actualPrice);

                        //构建orderStatusText:
                        //eg:"orderStatusText": "已付款";
                        //eg:"orderStatusText": "订单取消，退款中"
                        //根据order_status的状态码进行显示
                        //判断
                        //新建了一个表market_order_handleoption
                        //1.获取状态码
                        Short orderStatus = marketOrder.getOrderStatus();
                        //从刚刚创建的表中获取获取orderStatusText和handleOption
                        //通过orderStatus去差信息
                        MarketOrderHandleOption option = marketOrderMapper.selectAllHandlerOption(orderStatus);
                        String orderStatusText = option.getOrderStatusText();
                        wxOrderListVo.setOrderStatusText(orderStatusText);

                        //封装handleOption
                        Integer cancel = option.getCancel();
                        Integer delete = option.getDelete();
                        Integer pay = option.getPay();
                        Integer comment = option.getComment();
                        Integer confirm = option.getConfirm();
                        Integer refund = option.getRefund();
                        Integer reBuy = option.getReBuy();
                        Integer afterSale = option.getAfterSale();

                        optionVo = intAndBoolean(optionVo, cancel, delete, pay, comment, confirm, refund, reBuy, afterSale);
                        wxOrderListVo.setHandleOption(optionVo);

                        //根据id去order_goods中查找信息,order_id
                        MarketOrderGoodsExample orderGoodsExample = new MarketOrderGoodsExample();
                        MarketOrderGoodsExample.Criteria criteria1 = orderGoodsExample.createCriteria();
                        criteria1.andOrderIdEqualTo(id);
                        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(orderGoodsExample);
                        //封装
                        wxOrderListVo.setGoodsList(marketOrderGoods);

                        //添加到集合
                        data.add(wxOrderListVo);
                    }
                }
            }

        } else {
            //根据showType进行模糊查询  order
            //对showType做处理
            int a = showType * 100;

            Integer statusLow = a;
            Integer statusTop = statusLow + 100;
            MarketOrderExample example = new MarketOrderExample();
            MarketOrderExample.Criteria criteria = example.createCriteria();
            criteria.andUserIdEqualTo(userId);
            criteria.andOrderStatusBetween(statusLow, statusTop);

            list = marketOrderMapper.selectByExample(example);
            for (MarketOrder marketOrder : list) {
                //创建响应的对象
                WxOrderListVo wxOrderListVo = new WxOrderListVo();
                //创建响应体中WxOrderListHandOptionVo对象
                WxOrderListHandleOptionVo optionVo = new WxOrderListHandleOptionVo();
                //获取订单号
                Integer id = marketOrder.getId();
                wxOrderListVo.setId(id);
                //获取afterSaleStatus
                wxOrderListVo.setAftersaleStatus(marketOrder.getAftersaleStatus());
                //通过grouponPrice生成isGroupin
                BigDecimal grouponPrice = marketOrder.getGrouponPrice();
                String s = grouponPrice.toString();
                if (s.equals("0.00")) {
                    wxOrderListVo.setIsGroupin(false);
                }
                //orderSn
                String orderSn = marketOrder.getOrderSn();
                wxOrderListVo.setOrderSn(orderSn);
                //actualPrice
                BigDecimal actualPrice = marketOrder.getActualPrice();
                wxOrderListVo.setActualPrice(actualPrice);

                //构建orderStatusText:
                //eg:"orderStatusText": "已付款";
                //eg:"orderStatusText": "订单取消，退款中"
                //根据order_status的状态码进行显示
                //判断
                //新建了一个表market_order_handleoption
                //1.获取状态码
                Short orderStatus = marketOrder.getOrderStatus();
                //从刚刚创建的表中获取获取orderStatusText和handleOption
                //通过orderStatus去差信息
                MarketOrderHandleOption option = marketOrderMapper.selectAllHandlerOption(orderStatus);
                String orderStatusText = option.getOrderStatusText();
                wxOrderListVo.setOrderStatusText(orderStatusText);

                //封装handleOption
                Integer cancel = option.getCancel();
                Integer delete = option.getDelete();
                Integer pay = option.getPay();
                Integer comment = option.getComment();
                Integer confirm = option.getConfirm();
                Integer refund = option.getRefund();
                Integer reBuy = option.getReBuy();
                Integer afterSale = option.getAfterSale();

                optionVo = intAndBoolean(optionVo, cancel, delete, pay, comment, confirm, refund, reBuy, afterSale);
                wxOrderListVo.setHandleOption(optionVo);

                //根据id去order_goods中查找信息,order_id
                MarketOrderGoodsExample orderGoodsExample = new MarketOrderGoodsExample();
                MarketOrderGoodsExample.Criteria criteria1 = orderGoodsExample.createCriteria();
                criteria1.andOrderIdEqualTo(id);
                List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(orderGoodsExample);
                //封装
                wxOrderListVo.setGoodsList(marketOrderGoods);

                //添加到集合
                data.add(wxOrderListVo);
            }

        }
        //开启分页
        //PageHelper.startPage(info.getPage(), info.getLimit());
        //分页
        PageInfo<MarketOrder> pageInfo = new PageInfo<>(list);
        return new CommonData<>(
                (int) pageInfo.getTotal(),
                pageInfo.getPages(),
                info.getLimit(),
                info.getPage(),
                data
        );
    }

    /**
     * 根据orderId显示订单详情
     *
     * @param orderId
     * @return
     */
    @Override
    public WxOrderDetailVo wxDetail(Integer orderId) {
        //创建响应对象
        WxOrderDetailVo data = new WxOrderDetailVo();
        //创建响应体中handleOption
        WxOrderListHandleOptionVo optionVo = new WxOrderListHandleOptionVo();

        //根据orderId去order表中查数据
        //TODO  这里做了强转，不确定是否能行，若是不行的话，先获取给MarketOrder，然后一个个赋值
        //WxOrderDetailInfoVo wxOrderDetailInfoVo = (WxOrderDetailInfoVo) marketOrderMapper.selectByPrimaryKey(orderId);
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderId);
        //强转不了，因为父类不能强转为子类
        //下面一点点的的给Vo赋值
        WxOrderDetailInfoVo wxOrderDetailInfoVo = new WxOrderDetailInfoVo();

        //经测试，需要重新赋值
//        System.out.println(wxOrderDetailInfoVo);
//        String consignee = wxOrderDetailInfoVo.getConsignee();
//        System.out.println(consignee);
        //一点点赋值
        wxOrderDetailInfoVo.setConsignee(marketOrder.getConsignee());
        wxOrderDetailInfoVo.setAddress(marketOrder.getAddress());
        wxOrderDetailInfoVo.setAddTime(marketOrder.getAddTime());
        wxOrderDetailInfoVo.setOrderSn(marketOrder.getOrderSn());
        wxOrderDetailInfoVo.setActualPrice(marketOrder.getActualPrice());

        wxOrderDetailInfoVo.setMobile(marketOrder.getMobile());
        wxOrderDetailInfoVo.setMessage(marketOrder.getMessage());
        wxOrderDetailInfoVo.setOrderStatus(marketOrder.getOrderStatus());
        wxOrderDetailInfoVo.setAftersaleStatus(marketOrder.getAftersaleStatus());
        wxOrderDetailInfoVo.setGoodsPrice(marketOrder.getGoodsPrice());
        wxOrderDetailInfoVo.setCouponPrice(marketOrder.getCouponPrice());
        wxOrderDetailInfoVo.setId(marketOrder.getId());
        wxOrderDetailInfoVo.setFreightPrice(marketOrder.getFreightPrice());

        //获取订单状态码
        Short orderStatus = wxOrderDetailInfoVo.getOrderStatus();

        //根据订单状态码去新建的表中进行查询
        MarketOrderHandleOption option = marketOrderMapper.selectAllHandlerOption(orderStatus);
        String orderStatusText = option.getOrderStatusText();
        //赋值
        wxOrderDetailInfoVo.setOrderStatusText(orderStatusText);

        //封装handleOption
        Integer cancel = option.getCancel();
        Integer delete = option.getDelete();
        Integer pay = option.getPay();
        Integer comment = option.getComment();
        Integer confirm = option.getConfirm();
        Integer refund = option.getRefund();
        Integer reBuy = option.getReBuy();
        Integer afterSale = option.getAfterSale();

        optionVo = intAndBoolean(optionVo, cancel, delete, pay, comment, confirm, refund, reBuy, afterSale);
        wxOrderDetailInfoVo.setHandleOption(optionVo);

        //给响应体中的wxOrderDetailInfoVo赋值
        data.setOrderInfo(wxOrderDetailInfoVo);

        //TODO  根据orderId获取orderGoods信息
        MarketOrderGoodsExample example = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        List<MarketOrderGoods> list = marketOrderGoodsMapper.selectByExample(example);

        //给响应体中的orderGoods赋值
        data.setOrderGoods(list);
        return data;
    }

    /**
     * 根据id进行取消订单，订单状态码有101：未付款，改成102：用户取消
     * 并且返还优惠券
     *
     * @param orderId
     */
    @Override
    public void wxCancel(Integer orderId) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        short status = 102;
        marketOrder.setOrderStatus(status);
        Date date = new Date();
        marketOrder.setUpdateTime(date);
        //marketOrderMapper.updateByPrimaryKey(marketOrder);
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);


        //返还优惠券，根据orderId去coupon_user中修改优惠券状态
        int id = marketOrderMapper.selectCouponUserIdByOrderId(orderId);
        //获取到id之后修改status为0
        MarketCouponUser marketCouponUser = new MarketCouponUser();
        marketCouponUser.setId(id);
        short a = 0;
        marketCouponUser.setStatus(a);
        marketCouponUser.setUpdateTime(date);
        marketCouponUserMapper.updateByPrimaryKeySelective(marketCouponUser);

    }

    /**
     * 根据id进行申请退款，订单状态码....202：申请退款
     *
     * @param orderId
     */
    @Override
    public void wxRefund(Integer orderId) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        short status = 202;
        marketOrder.setOrderStatus(status);
        Date date = new Date();
        marketOrder.setUpdateTime(date);
        //marketOrderMapper.updateByPrimaryKey(marketOrder);
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
    }


    /**
     * 根据orderId进行逻辑删除，修改deleted值为1
     *
     * @param orderId
     */
    @Override
    public void wxDelete(Integer orderId) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        marketOrder.setDeleted(true);
        Date date = new Date();
        marketOrder.setUpdateTime(date);
        //marketOrderMapper.updateByPrimaryKey(marketOrder);
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);

    }

    /**
     * 确认收货，修改状态码：401
     * 改变comments数量
     * @param orderId
     */
    @Override
    public void wxConfirm(Integer orderId) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        short status = 401;
        //TODO 改变comments数量
        //根据orderId去order_goods查集合
        MarketOrderGoodsExample goodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        List<MarketOrderGoods> list = marketOrderGoodsMapper.selectByExample(goodsExample);
        int size = list.size();
        short count=(short) size;
        marketOrder.setComments(count);
        marketOrder.setOrderStatus(status);
        Date date = new Date();
        marketOrder.setUpdateTime(date);
        //确认收货时间
        marketOrder.setConfirmTime(date);
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);

    }

    /**
     * 确认收货后，商品评价时，商品信息的回显
     * 根绝orderId,goodsId一起去查找
     * 从order_goods表中获取数据
     *
     * @param orderId
     * @param goodsId
     * @return
     */
    @Override
    public MarketOrderGoods wxGoods(Integer orderId, Integer goodsId) {

        //根据orderId,goodsId去查表中的主键id
        int id = marketOrderGoodsMapper.selectIdByOrderIdAndGoodsId(orderId, goodsId);
        MarketOrderGoods data = marketOrderGoodsMapper.selectByPrimaryKey(id);
        return data;
    }


    /**
     * 临时方法，后面sql中处理就可以删掉
     *
     * @param optionVo
     * @param cancel
     * @param delete
     * @param pay
     * @param comment
     * @param confirm
     * @param refund
     * @param reBuy
     * @param afterSale
     * @return
     */
    private WxOrderListHandleOptionVo intAndBoolean(WxOrderListHandleOptionVo optionVo, Integer cancel, Integer delete, Integer pay, Integer comment, Integer confirm, Integer refund, Integer reBuy, Integer afterSale) {
        if (cancel == 0) {
            optionVo.setCancel(false);
        } else {
            optionVo.setCancel(true);
        }
        if (delete == 0) {
            optionVo.setDelete(false);
        } else {
            optionVo.setDelete(true);
        }
        if (pay == 0) {
            optionVo.setPay(false);
        } else {
            optionVo.setPay(true);
        }
        if (comment == 0) {
            optionVo.setComment(false);
        } else {
            optionVo.setComment(true);
        }
        if (confirm == 0) {
            optionVo.setConfirm(false);
        } else {
            optionVo.setConfirm(true);
        }
        if (refund == 0) {
            optionVo.setRefund(false);
        } else {
            optionVo.setRefund(true);
        }
        if (reBuy == 0) {
            optionVo.setRebuy(false);
        } else {
            optionVo.setRebuy(true);
        }
        if (afterSale == 0) {
            optionVo.setAftersale(false);
        } else {
            optionVo.setAftersale(true);
        }
        return optionVo;
    }

    /**
     * 前台用户相关信息的显示
     * @return
     */
    @Override
    public WxUserIndexBo count() {
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        if(principals!=null){
            MarketUser primaryPrincipal1 = (MarketUser) principals.getPrimaryPrincipal();
            Integer userId = primaryPrincipal1.getId();
            WxUserIndexBo wxUserIndexBo = new WxUserIndexBo();
            //首先统计未付款
            MarketOrderExample marketOrderExample = new MarketOrderExample();
            MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
            criteria.andOrderStatusBetween(100,199);
            criteria.andUserIdEqualTo(userId);
            long unpaid= marketOrderMapper.countByExample(marketOrderExample);
            //接下来统计待发货
            Integer lowStatusTwo=200;
            Integer topStatusTwo=300;
            int unShip=marketOrderMapper.countUnReceive(lowStatusTwo,topStatusTwo,userId);
            //接下来统计待收货
            Integer lowStatusThree=300;
            Integer topStatusThree=400;
            int unReceive=marketOrderMapper.countUnReceive(lowStatusThree,topStatusThree,userId);
            //最后统计待评价
            List<Integer> list = new ArrayList<>();
            list=marketOrderMapper.countComments(userId);
            int unComment=0;
            for (Integer integer : list) {
                unComment+=integer;
            }
            // long uncomment = marketOrderGoodsMapper.countByExample(marketOrderGoodsExample);

            WxUserIndexBo order = new WxUserIndexBo( unComment, (int) unpaid, unReceive, unShip);
            return order;

        }
        WxUserIndexBo order = new WxUserIndexBo( 0, 0, 0, 0);
        return order;

    }

    /**
     * 用户支付：修改订单状态
     * @param orderId
     */
    @Override
    public void wxPrepay(Integer orderId) {
        //根据orderId,修改订单状态
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        short a=201;
        marketOrder.setOrderStatus(a);
        Date date = new Date();
        marketOrder.setUpdateTime(date);
        marketOrder.setPayTime(date);
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);

    }
}






